package com.flyco.modules.system.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.flyco.common.api.vo.Result;
import com.flyco.common.system.query.QueryGenerator;
import com.flyco.common.aspect.annotation.AutoLog;
import com.flyco.common.system.vo.LoginUser;
import com.flyco.common.util.oConvertUtils;
import com.flyco.modules.system.entity.SysUserStore;
import com.flyco.modules.system.model.StoreInfoModel;
import com.flyco.modules.system.service.ISysUserStoreService;

import java.util.Date;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import com.flyco.common.system.base.controller.BaseController;
import com.flyco.poi.excel.ExcelImportUtil;
import com.flyco.poi.excel.def.NormalExcelConstants;
import com.flyco.poi.excel.entity.ExportParams;
import com.flyco.poi.excel.entity.ImportParams;
import com.flyco.poi.excel.view.EntityExcelView;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @Description: 人员经销商关系表
 * @Author: flyco
 * @Date: 2024-07-30
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "人员经销商关系表")
@RestController
@RequestMapping("/f/system/sysUserStore")
public class SysUserStoreController extends BaseController<SysUserStore, ISysUserStoreService> {
    @Autowired
    private ISysUserStoreService sysUserStoreService;

    /**
     * 分页列表查询
     *
     * @param sysUserStore
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "人员经销商关系表-分页列表查询")
    @ApiOperation(value = "人员经销商关系表-分页列表查询", notes = "人员经销商关系表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(SysUserStore sysUserStore,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<SysUserStore> queryWrapper = QueryGenerator.initQueryWrapper(sysUserStore, req.getParameterMap());
        Page<SysUserStore> page = new Page<SysUserStore>(pageNo, pageSize);
        IPage<SysUserStore> pageList = sysUserStoreService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @param sysUserStore
     * @return
     */
    @AutoLog(value = "人员经销商关系表-添加")
    @ApiOperation(value = "人员经销商关系表-添加", notes = "人员经销商关系表-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody SysUserStore sysUserStore) {
        sysUserStoreService.saveWithStore(sysUserStore);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param sysUserStore
     * @return
     */
    @AutoLog(value = "人员经销商关系表-编辑")
    @ApiOperation(value = "人员经销商关系表-编辑", notes = "人员经销商关系表-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody SysUserStore sysUserStore) {
        sysUserStoreService.saveWithStore(sysUserStore);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "人员经销商关系表-通过id删除")
    @ApiOperation(value = "人员经销商关系表-通过id删除", notes = "人员经销商关系表-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        sysUserStoreService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "人员经销商关系表-批量删除")
    @ApiOperation(value = "人员经销商关系表-批量删除", notes = "人员经销商关系表-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.sysUserStoreService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "人员经销商关系表-通过id查询")
    @ApiOperation(value = "人员经销商关系表-通过id查询", notes = "人员经销商关系表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id, @RequestParam(name = "partnerId") Long partnerId) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        Long ptId = sysUser.getPartnerId();
        if (partnerId != null && partnerId > 0) {
            ptId = partnerId;
        }
        SysUserStore sysUserStore = sysUserStoreService.getByUserId(id, ptId);
        return Result.ok(sysUserStore);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param sysUserStore
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, SysUserStore sysUserStore) {
        return super.exportXls(request, sysUserStore, SysUserStore.class, "人员经销商关系表");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, SysUserStore.class);
    }

    /**
     * 根据渠道，经销商名称查询经销商
     *
     * @param channelIds
     * @param storeName
     * @return
     */
    @AutoLog(value = "根据渠道，经销商名称查询经销商")
    @ApiOperation(value = "根据渠道，经销商名称查询经销商", notes = "根据渠道，经销商名称查询经销商")
    @GetMapping(value = "/queryStore")
    public Result<?> queryStore(@RequestParam(name = "channelIds") String channelIds, @RequestParam(name = "storeName") String storeName, @RequestParam(name = "partnerId") Long partnerId, @RequestParam(name = "officeIds") String officeIds) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        //默认取当前用户，如果有传入取传入值
        Long ptId = sysUser.getPartnerId();
        if (partnerId != null && partnerId > 0) {
            ptId = partnerId;
        }
        List<StoreInfoModel> storeInfoList = sysUserStoreService.getStore(channelIds, storeName, ptId, officeIds);
        return Result.ok(storeInfoList);
    }

}
